/*
Copyright (c) 2008  Franklin Schmidt <fschmidt@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

package fschmidt.tools;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.File;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.util.Set;
import java.util.HashSet;
import java.util.List;
import java.util.ArrayList;
import java.util.Date;
import java.util.regex.Pattern;


/**
 * Doing "java fschmidt.tools.Mmake" on the command line will build
 * make files in the current directory and all sub-directories.
 *
 * @author  frank
 */
public final class Mmake {
	private static String compiler = "javac -g -encoding UTF8";
	private static String toolsPath;
	private static String[] classpath = System.getProperty("java.class.path").split(Pattern.quote(File.pathSeparator),-1);

	static String findPathTo(String file) {
		for( String s : classpath ) {
			File path = new File(s);
			if( path.isAbsolute() && new File(path,file).exists() )
				return path.getPath() + File.separator;
		}
		return null;
	}

	public static void main(String args[]) throws Exception {
		for( int i=0; i<args.length; i++ ) {
			compiler += " " + args[i];
		}
		String subpath = "fschmidt"+File.separator+"tools";
		String path = findPathTo(subpath+File.separator+"Mmake.java");
		if( path != null ) {
			toolsPath = (path+subpath).replace('\\','/');			
		}
		mmake(".");
	}

	private static class R {
		boolean java = false;
		boolean j_p = false;
		boolean rmi = false;
		boolean jjt = false;
	}

	static R mmake(String dir)
		throws IOException
	{
		R rtn = new R();
		PrintWriter out = null;
		File dirF = new File(dir);
		String[] ls = dirF.list();
		if( ls == null )
			return rtn;
		Set<String> java = new HashSet<String>();
		Set<String> jtp = new HashSet<String>();
		Set<String> jmp = new HashSet<String>();
		Set<String> rmi = new HashSet<String>();
		Set<String> jjt = new HashSet<String>();
		List<String> dirs = new ArrayList<String>();
		List<String> dirs2 = new ArrayList<String>();
		List<String> dirsRmi = new ArrayList<String>();
		List<String> dirsJjt = new ArrayList<String>();
		for( int i=0; i<ls.length; i++ ) {
			String file = ls[i];
			if( file.endsWith(".java") ) {
				String s = file.substring(0,file.length()-5);
				java.add(s);
			}
			if( file.endsWith(".jtp") ) {
				String s = file.substring(0,file.length()-4);
				java.add(s);
				jtp.add(s);
			}
			if( file.endsWith(".jmp") ) {
				String s = file.substring(0,file.length()-4);
				java.add(s);
				jmp.add(s);
			}
			if( file.endsWith(".rmi") ) {
				String s = file.substring(0,file.length()-4);
				rmi.add(s);
			}
			if( file.endsWith(".jjt") ) {
				String s = file.substring(0,file.length()-4);
				jjt.add(s);
			}
			String d = dir + "/" + file;
			if( new File(d).isDirectory() ) {
				R r = mmake(d);
				if( r.java ) {
					dirs.add(file);
					if( r.j_p )
						dirs2.add(file);
					if( r.rmi )
						dirsRmi.add(file);
					if( r.jjt )
						dirsJjt.add(file);
				}
			}
		}
		if( java.isEmpty() && dirs.isEmpty() )
			return rtn;
		rtn.java = true;
		String[] a;
		out = init(dir);
		if( !java.isEmpty() ) {
			a = (String[])java.toArray(new String[0]);
			for( int i=0; i<a.length; i++ ) {
				out.println("\\");
				out.print("\t\t"+a[i]+".class ");
			}
		}
		out.println();
		String[] subs = (String[])dirs.toArray(new String[0]);
		int n = subs.length;
		for( int i=0; i<n; i++ )
			out.println("\tcd `pwd`/"+subs[i]+";  make _src");

		rtn.j_p = !jtp.isEmpty() || !jmp.isEmpty() || !dirs2.isEmpty();
		out.println();
		if( !rtn.j_p ) {
			out.println("_java:");
		} else {
			if( toolsPath==null ) {
				out.print("_java:\t");
			} else {
				boolean hasJtp = !jtp.isEmpty();
				boolean hasJmp = !jmp.isEmpty();
				if( hasJtp ) {
					out.println(toolsPath+"/Jtp.class: "+toolsPath+"/Jtp.java");
					out.println("\tjavac "+toolsPath+"/Jtp.java");
					out.println();
				}
				if( hasJmp ) {
					out.println(toolsPath+"/Jmp.class: "+toolsPath+"/Jmp.java");
					out.println("\tjavac "+toolsPath+"/Jmp.java");
					out.println();
				}
				out.print("_java:\t");
				if( hasJtp )
					out.print(toolsPath+"/Jtp.class ");
				if( hasJmp )
					out.print(toolsPath+"/Jmp.class ");
			}
			a = (String[])jtp.toArray(new String[0]);
			for( int i=0; i<a.length; i++ ) {
				out.println("\\");
				out.print("\t\t"+a[i]+".java ");
			}
			a = (String[])jmp.toArray(new String[0]);
			for( int i=0; i<a.length; i++ ) {
				out.println("\\");
				out.print("\t\t"+a[i]+".java ");
			}
			out.println();
			String[] subs2 = (String[])dirs2.toArray(new String[0]);
			int n2 = subs2.length;
			for( int i=0; i<n2; i++ )
				out.println("\tcd `pwd`/"+subs2[i]+";  make _java");
		}

		rtn.rmi = !rmi.isEmpty() || !dirsRmi.isEmpty();
		out.println();
		out.print("_rmi: ");
		if( !rtn.rmi ) {
			out.println();
		} else {
			a = (String[])rmi.toArray(new String[0]);
			for( int i=0; i<a.length; i++ ) {
				String s = a[i];
				out.println("\\");
				out.print("\t\t"+s+"_Stub.class ");
			}
			out.println();
			String absPath = dirF.getCanonicalPath();
			File cPath = null;
			for( String path : classpath ) {
				File f = new File(path);
				if( !f.isAbsolute() )
					continue;
				if( absPath.startsWith(f.getCanonicalPath()) ) {
					cPath = f.getCanonicalFile();
					break;
				}
			}
			if( cPath==null )
				throw new RuntimeException(dir+" not in classpath");
			StringBuilder clsPathBuf = new StringBuilder();
			StringBuilder topBuf = new StringBuilder( "." );
			for( File f=dirF.getCanonicalFile(); !f.equals(cPath); f=f.getParentFile() ) {
				clsPathBuf.insert(0, f.getName() + '.' );
				topBuf.append( File.separator + ".." );
			}
			String clsPath = clsPathBuf.toString();
			String top = topBuf.toString();
			for( int i=0; i<a.length; i++ ) {
				String s = a[i];
				out.println();
				out.println(s+"_Stub.class: "+s+".class");
				out.println("\trmic -d '"+top+"' "+clsPath+s);
			}
			String[] subs2 = (String[])dirsRmi.toArray(new String[0]);
			int n2 = subs2.length;
			for( int i=0; i<n2; i++ )
				out.println("\tcd `pwd`/"+subs2[i]+";  make _rmi");
		}

		rtn.jjt = !jjt.isEmpty() || !dirsJjt.isEmpty();
		out.println();
		out.print("_jjt: ");
		if( rtn.jjt ) {
			a = (String[])jjt.toArray(new String[0]);
			for( int i=0; i<a.length; i++ ) {
				out.println("\\");
				out.print("\t\t"+a[i]+".jjt_done ");
			}
			out.println();
			String[] subs2 = (String[])dirsJjt.toArray(new String[0]);
			int n2 = subs2.length;
			for( int i=0; i<n2; i++ )
				out.println("\tcd `pwd`/"+subs2[i]+";  make _jjt");
		}

		out.println();
		out.println("clean:");
		out.print("\trm -f *.class *.jjt_done");
		a = (String[])jtp.toArray(new String[0]);
		for( int i=0; i<a.length; i++ ) {
			out.print(" "+a[i]+".java");
		}
		a = (String[])jmp.toArray(new String[0]);
		for( int i=0; i<a.length; i++ ) {
			out.print(" "+a[i]+".java");
		}
		out.println();
		for( int i=0; i<n; i++ )
			out.println("\tcd `pwd`/"+subs[i]+";  make clean");
		out.println();
		out.close();
		System.out.println(dir);

		return rtn;
	}

	static PrintWriter init(String dir) 
		throws IOException
	{
		PrintWriter out = new PrintWriter(new BufferedOutputStream(
				new FileOutputStream(dir+"/Makefile")
			));
		out.println("# Makefile created on "+new Date()+"  by Mmake");
		out.println();
		out.println(".SUFFIXES: .jtp .jmp .java .class .jjt .jjt_done");
		out.println();
		out.println(".java.class:");
		out.println("\t"+compiler+" $<");
		out.println();
		out.println(".jtp.java:");
		out.println("\tjava fschmidt.tools.Jtp $<");
		out.println();
		out.println(".jmp.java:");
		out.println("\tjava fschmidt.tools.Jmp $<");
		out.println();
		out.println(".jjt.jjt_done:");
		out.println("\tjava jjtree $<");
		out.println("\tjava javacc $*.jj");
		out.println("\ttouch $@");
		out.println();
		out.println("all: _jjt _java _src _rmi");
		out.println();
		out.println("hlp:");
		out.println("\t@echo \"Usage: make [all|clean]\"");
		out.println();
		out.print("_src: ");
		return out;
	}
}
